Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nonlinear constraints with Boolean operators #21

Merged
merged 3 commits into from
Jan 26, 2023
Merged

Conversation

odow
Copy link
Member

@odow odow commented Jan 26, 2023

x-ref: jump-dev/JuMP.jl#2227 (comment)

The demo is

julia> using JuMP

julia> import MiniZinc

julia> model = Model(() -> MiniZinc.Optimizer{Int}(MiniZinc.Chuffed()))
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: EMPTY_OPTIMIZER
Solver name: MiniZinc

julia> set_optimizer_attribute(model, "model_filename", "test.mzn")

julia> @variable(model, x, Bin)
x

julia> @variable(model, y, Bin)
y

julia> @constraint(model, [x, y] in MOI.AllDifferent(2))
[x, y]  MathOptInterface.AllDifferent(2)

julia> @NLconstraint(model, (x || y) == true)
(x || y) - 1.0 = 0

julia> @NLconstraint(model, (x && y) == false)
(x && y) - 0.0 = 0

julia> optimize!(model)
Warning: included file "count.mzn" overrides a global constraint file from the standard library. This is deprecated. For a solver-specific redefinition of a global constraint, override "fzn_<global>.mzn" instead.

Warning: included file "global_cardinality_low_up.mzn" overrides a global constraint file from the standard library. This is deprecated. For a solver-specific redefinition of a global constraint, override "fzn_<global>.mzn" instead.


julia> value(x), value(y)
(0.0, 1.0)

julia> print(read("test.mzn", String))
include "alldifferent.mzn";
var bool: x;
var bool: y;
constraint alldifferent([x, y]);
constraint ((x \/ y) - 1) == false;
constraint ((x /\ y) - 0) == false;
solve satisfy;

@codecov
Copy link

codecov bot commented Jan 26, 2023

Codecov Report

Base: 95.07% // Head: 95.15% // Increases project coverage by +0.07% 🎉

Coverage data is based on head (ca90a3a) compared to base (e31983b).
Patch coverage: 95.58% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #21      +/-   ##
==========================================
+ Coverage   95.07%   95.15%   +0.07%     
==========================================
  Files           3        3              
  Lines         386      454      +68     
==========================================
+ Hits          367      432      +65     
- Misses         19       22       +3     
Impacted Files Coverage Δ
src/write.jl 98.59% <95.16%> (-0.73%) ⬇️
src/MiniZinc.jl 100.00% <100.00%> (ø)
src/optimize.jl 81.31% <100.00%> (+0.41%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

# a || (b && (y < 5))
MOI.Nonlinear.add_constraint(
nlp,
:(($a || ($b && ($y < 5)))),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chriscoey nested expressions like this work, for both binary and integer variables.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!! 🤩

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll merge so that you can experiment with it more easily. It'll have some rough edges (assertion errors) when things go wrong, but we can fix those in time if there is interest.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome yes i plan to experiment with it in the next few days. thanks @odow!

@odow odow merged commit 56f1743 into master Jan 26, 2023
@odow odow deleted the od/boolean branch January 26, 2023 23:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants